package com.cjj.stop_thread;

import javafx.scene.paint.Stop;

/**
 *
 */
class StopThread_01 extends Thread{

    public boolean flag = true;

    @Override
    public synchronized void run() {
        while (flag){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
                stopThread();
            }
            System.out.println(Thread.currentThread().getName() + "----我是子线程");
        }
    }

    public void stopThread(){
        System.out.println(getName() + "----线程被停止");
        flag = false;
    }
}

/**
 * @ClassName StopThread
 * @Date 2021/07/17 17:46:49
 * @Created by ccc-ju
 * @email ccc-ju@outlook.com
 */
public class StopThread {

    public static void main(String[] args) {
        StopThread_01 stopThread_01 = new StopThread_01();
        StopThread_01 stopThread_02 = new StopThread_01();
        stopThread_01.start();
        stopThread_02.start();
        for (int i = 0; i < 30; i++){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("main......" + i);
            if(i == 29){
                stopThread_01.interrupt();
                stopThread_02.interrupt();
            }
        }
    }

}
